<?php

/**
 * This is the model class for table "{{mem_report}}".
 *
 * The followings are the available columns in table '{{mem_report}}':
 * @property string $zmr_id
 * @property integer $zmr_type
 * @property integer $zmr_type_info
 * @property integer $zm_id_from
 * @property integer $zm_id_to
 * @property string $zp_id
 * @property integer $zmr_nums
 * @property string $zo_id
 * @property string $zmr_title
 * @property string $zmr_content
 * @property string $zmr_reply
 * @property string $zmr_mem_pic
 * @property string $zmr_cmp_pic
 * @property integer $zad_id
 * @property integer $zmr_disposetime
 * @property string $zmr_disposeidea
 * @property integer $zmr_del
 * @property integer $zmr_status
 */
class MemReport extends CActiveRecord
{
	public $status = array(
					1=>'未处理',
					2=>'取证中',
					3=>'已处理',
			);
	
	//查询时间
	public $limit_time;
	
	//根据商品名称查询
	public $zp_name;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemReport the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_report}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
 			array('zmr_type_info,zm_id_from,zm_id_to,zp_id,zop_id,zmr_cmp_time,inputtime,zmr_disposetime', 'numerical', 'integerOnly'=>true),
  			array('zmr_content,zp_name', 'length','min'=>1),
			array('zmr_id', 'length','max'=>20),
  			//array('zmr_mem_pic', 'length','min'=>1),
 			//array('zmr_mem_pic','file','types'=>'jpg,gif,png','maxSize'=>'1024 * 500','tooLarge'=>'图片最大不超过1MB，请重新上传!'),
 		    array('zmr_id,zmr_type_info,zm_id_from,zm_id_to,zp_id,zmr_content','required','on'=>'reportthree'),
 		   	array('zmr_content, zmr_reply, zmr_disposeidea,zmr_mem_pic,zmr_cmp_pic', 'safe'),
			array('zmr_id, zmr_type, zmr_type_info, zm_id_from, zm_id_to, zp_id, zmr_nums, zo_id, zmr_title, zmr_content, zmr_reply, zmr_mem_pic, zmr_cmp_pic, zad_id, zmr_disposetime, zmr_disposeidea, zmr_del, zmr_status', 'safe', 'on'=>'search'),
		);
	}
    public function uniteonly(){
    	//限制会员一个产品一种类型只能举报一次
    	$oldreport = MemReport::findByAttributes(array('zp_id'=>$this->zp_id,
    										'zmr_type'=>2,
    										'zm_id_from'=>$this->zm_id_from,
    										'zmr_type_info'=>$this->zmr_type_info));
    	if(!empty($oldreport->zmr_id)>0){
    		throw new CHttpException('该产品已经举报过，请查看记录!');
    	}
    } 
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'buyer'=>array(self::BELONGS_TO,'Member',array('zm_id_from'=>'zm_id')),
				'product'=>array(self::BELONGS_TO,'Product','zp_id'),
				'order'=>array(self::BELONGS_TO,'Order','zo_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zmr_id' => '流水号',
			'zmr_type' => '类型',
			'zmr_type_info' => '操作类型',//1违背承诺,2描述不符,3未收到货,4售后保障服务,5出售禁售品6假货7知识产品侵权,8图片,9其他',
			'zm_id_from' => '发起会员ID',
			'zm_id_to' => '接收会员ID',
			'zp_id' => '产品ID',
			'zmr_nums' => '数量',
			'zo_id' => '订单号',
			'zmr_title' => '标题',
			'zmr_content' => '举报内容',
			'zmr_reply' => '商家解释',
			'zmr_cmp_time' => '商家解释时间',
			'zmr_mem_pic' => '会员图片证据URL',
			'zmr_cmp_pic' => '商家图片证据URL',
			'zad_id' => '管理员ID',
			'zmr_disposetime' => '处理时间',
			'zmr_disposeidea' => '处理意见',
			'zmr_del' => '是否删除',//1否2是
			'zmr_status' => '处理状态',//1未处理2取证中3已处理'
			'inputtime' => '创建时间',
			'zop_id' => '订单产品',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zmr_id',$this->zmr_id,true);
		$criteria->compare('zmr_type',$this->zmr_type);
		$criteria->compare('zmr_type_info',$this->zmr_type_info);
		$criteria->compare('zm_id_from',$this->zm_id_from);
		$criteria->compare('zm_id_to',$this->zm_id_to);
		
		$criteria->compare('zmr_nums',$this->zmr_nums);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('zmr_title',$this->zmr_title,true);
		$criteria->compare('zmr_content',$this->zmr_content,true);
		$criteria->compare('zmr_reply',$this->zmr_reply,true);
		$criteria->compare('zmr_cmp_time',$this->zmr_cmp_time);
		$criteria->compare('zmr_mem_pic',$this->zmr_mem_pic,true);
		$criteria->compare('zmr_cmp_pic',$this->zmr_cmp_pic,true);
		$criteria->compare('zad_id',$this->zad_id);
		$criteria->compare('zmr_disposetime',$this->zmr_disposetime);
		$criteria->compare('zmr_disposeidea',$this->zmr_disposeidea,true);
		$criteria->compare('zmr_del',$this->zmr_del);
		$criteria->compare('zmr_status',$this->zmr_status);
		
		//根据产品名称查询
		if(!empty($this->zp_name)){
			$criteria->join = ' LEFT JOIN {{product}} p ON p.zp_id=t.zp_id ';
			$criteria->addCondition(' p.zp_name like "%'.$this->zp_name.'%" ');
		}else{
			$criteria->compare('zp_id',$this->zp_id,true);
		}

		//根据时间查询
		if(!empty($this->limit_time)){
			if($this->limit_time==1){
				$criteria->addCondition(' inputtime  >'.(time()-3600*24*30));
			}elseif($this->limit_time==2){
				$criteria->addCondition(' inputtime  >'.(time()-3600*24*30*3));
			}elseif($this->limit_time==3){
				$criteria->addCondition(' inputtime  <'.(time()-3600*24*30*3));
			}
		}else{
			$criteria->compare('inputtime',$this->inputtime);
		}
		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>13,		
			),
		));
	}		
	
	//列表数据
	public function listSearch($type=2)
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zmr_type',$type);
		$criteria->compare('zmr_del',1);
		$criteria->compare('zm_id_from',Yii::app()->user->userid);
		$criteria->order = 'inputtime desc';
		
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array('pageSize'=>10),
		));
	}
	
	//反序列化图片数组
	public function afterFind(){
		parent::afterFind();
		$this->zmr_mem_pic = unserialize($this->zmr_mem_pic);
		$this->zmr_cmp_pic = unserialize($this->zmr_cmp_pic);
	}
	
	//列化图片数组
	public function beforeSave(){
		$this->zmr_mem_pic = serialize($this->zmr_mem_pic);
		$this->zmr_cmp_pic = serialize($this->zmr_cmp_pic);
		
		return true;
	}
	
	
	/**
	 *处理状态
	 */
	public function getZmrStatus($type=null){
		if($type==null){
			if(F::sitetype()==2)
				return array(
						''=>'请选择',
						'1'=>'等待处理',
						'3'=>'已处理',
				);
			else
				return array(
						''=>'请选择',
						'1'=>'买家申诉',
						'2'=>'卖家申诉',
						'3'=>'已处理',
				);
		}else{
			$level = $this->getZmrStatus();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 *投诉原因
	 */
	public function getTypeInfo($type=null){
		if($type==null){
			return array(
					'1'=>'违背承诺',
					'2'=>'描述不符',
					'3'=>'未收到货',
					'4'=>'售后保障服务',
					'5'=>'出售禁售品',
					'6'=>'假货',
					'7'=>'知识产品侵权',
					'8'=>'图片',
					'9'=>'其他',
			);
		}else{
			$level = $this->getTypeInfo();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 *成交时间
	 */
	public function getLimitTime($type=null){
		if($type==null){
			return array(
					''=>'请选择',
					'1'=>'最近一个月',
					'2'=>'最近三个月',
					'3'=>'三个月之前',
			);
		}else{
			$level = $this->getLimitTime();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
}